package com.content.view.Frame.CBar
{
	import com.content.AppFacade;
	import com.content.model.GlobalVarProxy;
	import com.content.model.LoadedAssets.ConfigProxy;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.CButton.Level1VO;
	import com.content.view.Component.CButton.Level2VO;
	import com.content.view.Frame.Content.Content;
	import com.content.view.Frame.MainPanel.MPBase;
	import com.library.core.IDisposable;
	
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class CBarMediator extends Mediator implements IMediator, IDisposable
	{
		public static const NAME:String							= 'CBarMediator';
		
		private var _view:CBar;
		
		public function CBarMediator(viewComponent:Object=null)
		{
			
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			_view = new CBar(	P_Config.UI_CBarL1_Width,
								P_Config.UI_CBarL1_MarginLeft,
								P_Config.UI_CBarL2_Width,
								P_Config.UI_CBarL2Btn_MarginH,
								P_Config.UI_CBarL2Btn_MarginV,
								P_Config.UI_CBarL2Btn_Height);			
			ParentView.addChild( _view );
			
			_view.addEventListener(CBarLevel1.CHOOSE_CBUTTON, OnChooseCButton1);
			_view.addEventListener(CBarLevel2.CHOOSE_CBUTTON, OnChooseCButton2);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				CBar.SHOW,
				CBar.HIDE,
				CBar.INIT,
				App.RESIZE_SYS,
				App.MOUSEUP
			];
		}
		
		/**
		 * Event Handler
		 */ 
		override public function handleNotification(note:INotification):void
		{
			var name:String = note.getName();
			var body:Object = note.getBody();
			
			switch ( name )
			{
				case CBar.SHOW:
					_view.ShowL1(body["delay"], body["duration"]);	
					break;
				case CBar.HIDE:
					_view.HideL1();		
					_view.HideL2();
					break;
				case CBar.INIT:
					_view.Init(P_Config.CBtnVOList, P_Skin);
					break;
				case App.RESIZE_SYS:
					var sHeight:int = body["height"];
					_view.SetLayout(sHeight);
					break;
				case App.MOUSEUP:
					var evt:MouseEvent = body["evt"];
					//if(evt.target is C)
					_view.HideL2();
					break;
			}
		}
		
		private function OnChooseCButton1(evt:CBarL1Event):void{
//			switch(evt.type)
//			{
//				case CBarLevel1.CHOOSE_CBUTTON:
					var l1VO:Level1VO = evt.L1VO;
					if(l1VO.MPID == "" && l1VO.Default == false){	// 如果没有MPID，并且不是默认，则打开子目录
						_view.ShowL2(l1VO);	// 打开子目录
					}
					// 如果没有 MPID，并且是默认，则不打开子目录，直接打开子面板。同时，将默认关闭
					// 此代码只执行一次
					else if(l1VO.MPID == "" && l1VO.Default == true){
						var l2VO:Level2VO;
						for each(l2VO in l1VO.SubVOList){
							if(l2VO.Default){
								sendNotification(AppFacade.MAINPANEL_OPEN, {MPID: l2VO.MPID});
								l1VO.Default = false;
								return;
							}
						}
					}
					else{
						sendNotification(AppFacade.MAINPANEL_OPEN, {MPID: l1VO.MPID});						
					}
//					break;
//			}
		}
		
		private function OnChooseCButton2(evt:DataEvent):void{
			sendNotification(AppFacade.MAINPANEL_OPEN, {MPID: evt.data});	
		}
		
		private function get ParentView():Sprite{return viewComponent as Sprite;}
		private function get P_Config():ConfigProxy	{return facade.retrieveProxy( ConfigProxy.NAME ) as ConfigProxy;}
		private function get P_Skin():SkinProxy	{return facade.retrieveProxy( SkinProxy.NAME ) as SkinProxy;}
		public function Dispose():void	{
			_view.removeEventListener(CBarLevel1.CHOOSE_CBUTTON, OnChooseCButton1);
			_view.removeEventListener(CBarLevel2.CHOOSE_CBUTTON, OnChooseCButton2);
			_view.Dispose();
		}
	}
}